package org.team4.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.team4.constants.SQLConstant;
import org.team4.domain.Dictionary;
import org.team4.exception.CRUDException;
import org.team4.util.Validate;

import org.team4.dao.DictionaryDAO;
import org.team4.dao.GenericDAO;

public class DictionaryDAO extends GenericDAO<Dictionary> {

	private static final long serialVersionUID = 6105230161780625369L;
	private static DictionaryDAO instance = null;
	
	public static DictionaryDAO getInstance(SessionFactory sessionFactory) {
        if(instance == null) {
            instance =  new DictionaryDAO(sessionFactory);
        }
        return instance;
    }
	
	private DictionaryDAO(SessionFactory sessionFactory) {
		super(sessionFactory, Dictionary.class);
	}

	/**
	 * ͬ���޸同步修改所有业务对象的字典字段值
	 * 
	 * @throws Exception
	 */
	public void syncUpdate(String superId, String newDictName, String oldDicName) throws CRUDException {
		List<String> updateList = SQLConstant.DICT_UPDATE_MAP.get(superId);
		if(Validate.collectionNotNull(updateList)) {
			for (String sql : updateList) {
				executeUpdate(sql, newDictName, oldDicName);
			}
		}
	}

	/**
	 * 查找某字典下最大编号
	 * 
	 * @param superId
	 * @return
	 * @throws CRUDException
	 */
	public Integer findMaxSortNum(String superId) throws CRUDException {
		try {
			return ((Integer) findUnique(SQLConstant.DICTIONARY_NEXT_SORTNUM, superId)) + 1;
		} catch (Exception e) {
			return 1;
		}
	}
}